<?php
class koudai_order_fzControl extends skymvc{
	
	public function __construct(){
		
		parent::__construct();	
	}
	
	public function onDefault(){
		$order_id=get_post('order_id','i');
		$order=M("koudai_order")->selectRow("order_id=".$order_id);
		
		$order_product=M("koudai_order_product")->select(array(
			"where"=>"order_id=".$order_id
		));
		if($order_product){
			foreach($order_product as $k=>$v){
				$productids[]=$v['productid'];
			}
			$products=M("product")->getListByIds($productids);
			foreach($order_product as $k=>$v){
				$p=$products[$v['productid']];
				$p['price']=$v['price'];
				$v['title']=$p['title'];
				$v['imgurl']=$p['imgurl'];
				$jznum=$this->jznum($v['order_id'],$v['productid']);
				$v['jznum']=$v['amount']-$jznum;
				$order_product[$k]=$v;
			}
		}
		$this->smarty->assign(array(
			"order"=>$order,
			"order_product"=>$order_product,
			"fzuser"=>M("koudai_order_fz")->selectRow("order_id=".$order_id." AND userid=".M("login")->userid." ")
		));
		$this->smarty->display("koudai_order_fz/index.html");
	}
	
	
	public function onFzSave(){
		$jz=post('jz','i');
		$order_id=get_post('order_id','i');
		$order=M("koudai_order")->selectRow("order_id=".$order_id);
		 
		$order_product=M("koudai_order_product")->select(array(
			"where"=>"order_id=".$order_id
		));
		
		if($order_product){
			foreach($order_product as $k=>$v){
				$productids[]=$v['productid'];
			}
			
			$products=M("product")->getListByIds($productids);
			$total_num=$total_money=0;
			foreach($order_product as $k=>$v){
				$p=$products[$v['productid']];			
				$jznum=$this->jznum($v['order_id'],$v['productid']);
				 
				if($jz[$v['productid']]>$v['amount']-$jznum){
					$this->goAll($p['title']."最多只能结账".($v['amount']-$jznum));
				}
				$v['jznum']=$jz[$v['productid']];
				$total_num+=$v['jznum'];
				$total_money+=$v['jznum']*$v['price'];
				$order_product[$k]=$v;
			}
			$fzuser=M("koudai_order_fz")->selectRow("order_id=".$order_id." AND userid=".M("login")->userid." ");
			if($fzuser){
				M("koudai_order_fz")->update(array(
						"num"=>$fzuser['num']+$total_num,
						"money"=>$fzuser['money']+$total_money,
					),"id=".$fzuser['id']);
			}else{
				M("koudai_order_fz")->insert(array(
						"order_id"=>$v['order_id'],
						"userid"=>M("login")->userid,
						"dateline"=>time(),
						"num"=>$total_num,
						"money"=>$total_money,
					));
			}
			foreach($order_product as $k=>$v){
				$row=M("koudai_order_fz_product")->selectRow("order_id=".$order_id." AND productid=".$v['productid']." AND userid=".M("login")->userid." ");
				if($row){
					M("koudai_order_fz_product")->update(array(
						"num"=>$row['num']+$v['jznum'],
						"money"=>$row['money']+$v['jznum']*$v['price'],
					),"id=".$row['id']);
				}else{
					M("koudai_order_fz_product")->insert(array(
						"order_id"=>$v['order_id'],
						"productid"=>$v['productid'],
						"userid"=>M("login")->userid,
						"dateline"=>time(),
						"num"=>$v['jznum'],
						"price"=>$v['price'],
						"money"=>$v['jznum']*$v['price'],
					));
				}
			}
		}
		
		/*对账记录*/
		/**
		M("koudai_dz_log")->insert(array(
			"order_id"=>$order_id,
			"userid"=>M("login")->userid,
			"sushe"=>$order['sushe'],
			"dateline"=>time(),
			"k"=>"fz_add"
		));
		**/
		/*End对账记录*/
		$this->goAll("结账成功");
	}
	
	public function jznum($order_id,$productid){
		return M("koudai_order_fz_product")->selectOne(array(
			"where"=>" order_id=".$order_id." AND productid=".$productid."",
			"fields"=>" sum(num) as num"
		));
		
	}
	
	public function onMy(){
		$order_id=get_post('order_id','i');
		$order=M("koudai_order")->selectRow("order_id=".$order_id);
		 
		$fzlist=M("koudai_order_fz")->select(array(
			"order_id"=>$order_id
		));
		if($fzlist){
			foreach($fzlist as $k=>$v){
				$uids[]=$v['userid'];
			}
			$us=M("user")->getUserByIds($uids);
			foreach($fzlist as $k=>$v){
				$v['nickname']=$us[$v['userid']]['nickname'];
				$v['products']=$this->getProductByfz($v['order_id'],$v['userid']);
				$fzlist[$k]=$v;
			}
		}
		
		$this->smarty->assign(array(
			"order"=>$order,
			"fzlist"=>$fzlist,
			"fzuser"=>M("koudai_order_fz")->selectRow("order_id=".$order_id." AND userid=".M("login")->userid." ")
		));
		$this->smarty->display("koudai_order_fz/my.html");
	}
	
	public function getProductByFz($order_id,$userid){
		$fzlist=M("koudai_order_fz_product")->select(array(
			"where"=>"order_id=".$order_id." AND userid=".$userid." "
		));
		if($fzlist){
			foreach($fzlist as $v){
				$productids[]=$v['productid'];
			}
			$products=M("product")->getListByIds($productids);
			
			foreach($fzlist as $k=>$v){
				$v['title']=$products[$v['productid']]['title'];
				$v['imgurl']=$products[$v['productid']]['imgurl'];
				$fzlist[$k]=$v;
					
			}
			
			return $fzlist;
		}
	}
	
}

?>